package com.deli.springboot.utils;

import org.apache.tomcat.util.security.MD5Encoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @Author: xudeli
 * @Description:
 * @CreateDate: 2018/4/22 23:19
 **/
public class MD5Utils {
    private static final Logger logger = LoggerFactory.getLogger(MD5Utils.class);

    public static String encoder(String pwd,String salt){
        try {
            StringBuffer stringBuffer = new StringBuffer();
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] digests = md5.digest((pwd + salt).getBytes());
            for (byte digest : digests) {
                int i = digest & 0xff;
                String hexString = Integer.toHexString(i);
                if (hexString.length() < 2) {
                    hexString = "0" + hexString;
                }
                stringBuffer.append(hexString);
            }
            logger.info("pwd:{}",stringBuffer.toString());
            return stringBuffer.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        encoder("123456","123");
    }
}
